<?php

namespace App\Repositories;

use Prettus\Repository\Eloquent\BaseRepository as Repository;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

class UtilRepository extends Repository
{    


    public function model()
    {
        return 'App\Models\Util';
    }


    /**
     * [验证码]
     * @return [type] [description]
     */
    public function captcha()
    {
        $phraseBuilder = new PhraseBuilder(4, '0123456789');

        $captcha = new CaptchaBuilder(null, $phraseBuilder);
        $captcha->setBackgroundColor(247, 247, 247);
        $captcha->setMaxBehindLines(0);
        $captcha->setMaxFrontLines(0);
        $captcha->build(110, 35);

        $phrase = $captcha->getphrase();

        \Session::put('captcha', $phrase);
        header('Cache-Control: no-cache, must-revalidate');
        header('Content-type: image/jpeg');

        return $captcha->output();
    }

    
    /**
     * [二次验证安全码]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public function safecode($request)
    {
        $safecode = $request->safecode;

        if ($safecode != SAFECODE) {
            return $this->responseFail(403, '安全码错误！');
        }

        return $this->responseSuccess('安全码正确', $safecode);
    }


    public function members($request)
    {
        $query = $request->term;
        
        $result = array();
        //匹配到全数字
        if (preg_match('/^\d+$/i', $query)) {
            if (strlen($query) < 4) {
                return null;
            }

            //按手机查找(四位数字按手机号/手机尾号)
            if (strlen($query) == 4 || strlen($query) == 11) {
                $rows = \App\Models\Mgt\Member::where('mobile', 'like', "%$query%")
                    ->orderBy('id', 'asc')
                    ->get();

                //为体现一户多卡的情况，需要格式转换
                if (!!$rows) {
                    foreach ($rows as $member) {
                        foreach ($member->normal() as $key=>$card) {
                            $member->card_id = $card->id;
                            $member->card_no = $card->card_no;
                            $member->available = number_format($card->available, 2, ".", ",");
                            $member->rate = $card->grade->rate;
                            $member->card_title = $card->grade->name;
                            $result[] = $member->toArray();
                        }
                    }
                }
            }
            //按会员卡号查找(大于四位数字按卡号)
            else {
                $rows = \App\Models\Mgt\Card::where('card_no', 'like', "%$query%")
                    ->where('status', STATUS_NORMAL)
                    ->orderBy('id', 'desc')
                    ->get();

                if (!!$rows) {
                    foreach ($rows as $card) {
                        $card->member->card_id = $card->id;
                        $card->member->card_no = $card->card_no;
                        $card->member->available = number_format($card->available, 2, ".", ",");
                        $card->member->rate = $card->grade->rate;
                        $card->member->card_title = $card->grade->name;
                        $result[] = $card->member->toArray();
                    }
                }
            }
        }
        //未匹配到数字，按姓名查找
        else {
            $rows = \App\Models\Mgt\Member::where('name', 'like', "%$query%")
                ->orderBy('id', 'desc')
                ->get();

            if (!!$rows) {
                foreach ($rows as $member) {
                    foreach ($member->normal() as $key=>$card) {
                        $member->card_id = $card->id;
                        $member->card_no = $card->card_no;
                        $member->available = number_format($card->available, 2, ".", ",");
                        $member->rate = $card->grade->rate;
                        $member->card_title = $card->grade->name;
                        $result[] = $member->toArray();
                    }
                }
            }
        }
        //dd($rows);
        return collection($result)->toJson();
    }


    public function sales($request)
    {
        $query = $request->term;
        $type = $request->type;
        
        //匹配到全数字，按手机查找
        if (preg_match('/^\d+$/i', $query)) {

            //按工号查找
            if (strlen($query) < 4) {
                $result = \App\Models\Mgt\Sales::where('workno', 'like', "%$query%")
                    ->where('type', $type)
                    ->orderBy('id', 'asc')
                    ->get();
            }

            if (strlen($query) >= 4) {
                $result = \App\Models\Mgt\Sales::where('mobile', 'like', "%$query%")
                    ->where('type', $type)
                    ->orderBy('id', 'desc')
                    ->get();
            }
        }
        //未匹配到数字，按姓名查找
        else {
            $result = \App\Models\Mgt\Sales::where('name', 'like', "%$query%")
                ->where('type', $type)
                ->orderBy('id', 'desc')
                ->get();
        }

        return $result->toJson();
    }


    public function changes($request)
    {
        $query = $request->term;
        
        if (preg_match('/^\d+$/i', $query)) {
            if (strlen($query) < 4) {
                return null;
            }

            $result = \App\Models\Biz\Change::where('id', 'like', "%$query%")
                    ->where('status', 1)
                    ->orderBy('id', 'desc')
                    ->get();
        }

        return $result->toJson();
    }

    

    


}
